<template>
  <div class="main-content">
    <h2 style="margin-bottom:20px;">库存管理 - 入库/出库登记</h2>
    <el-form :inline="false" :model="form" :rules="rules" ref="form" label-width="80px" style="max-width:600px;">
      <el-form-item label="物品名称" prop="name">
        <el-input v-model="form.name" placeholder="请输入物品名称" />
        </el-form-item>
      <el-form-item label="数量" prop="num">
        <el-input v-model.number="form.num" type="number" min="1" placeholder="请输入数量" />
          </el-form-item>
      <el-form-item label="类型" prop="type">
        <el-select v-model="form.type" placeholder="请选择类型">
          <el-option label="入库" value="in" />
          <el-option label="出库" value="out" />
        </el-select>
        </el-form-item>
      <el-form-item label="单位" prop="unit">
        <el-input v-model="form.unit" placeholder="如：件/箱/个" />
          </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input v-model="form.remark" placeholder="备注" />
        </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">登记</el-button>
        <el-button @click="resetForm">重置</el-button>
        <el-button @click="goBack">返回库存查询</el-button>
      </el-form-item>
    </el-form>
    <h3 style="margin:30px 0 10px 0;">入库/出库记录</h3>
    <el-table :data="dataList" style="width:100%" border>
      <el-table-column prop="name" label="物品名称" align="center" />
      <el-table-column prop="num" label="数量" align="center" />
      <el-table-column prop="type" label="类型" align="center">
        <template slot-scope="scope">
          <span v-if="scope.row.type==='in'">入库</span>
          <span v-else>出库</span>
        </template>
      </el-table-column>
      <el-table-column prop="unit" label="单位" align="center" />
      <el-table-column prop="time" label="时间" align="center" />
      <el-table-column prop="remark" label="备注" align="center" />
    </el-table>
    <el-pagination
      style="margin-top:20px;"
      :current-page="pageIndex"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, prev, pager, next, jumper"
      @current-change="currentChangeHandle"
      @size-change="sizeChangeHandle"
      :page-sizes="[10, 20, 50]"
      background
    />
  </div>
</template>
<script>
import { addRecord, getRecordList } from '@/utils/kucunLocal';
export default {
  data() {
    return {
      form: { name: '', num: 1, type: '', unit: '', remark: '' },
      rules: {
        name: [{ required: true, message: '请输入物品名称', trigger: 'blur' }],
        num: [{ required: true, message: '请输入数量', trigger: 'blur' }],
        type: [{ required: true, message: '请选择类型', trigger: 'change' }],
        unit: [{ required: true, message: '请输入单位', trigger: 'blur' }],
      },
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
    };
  },
  created() {
    this.getDataList();
  },
  methods: {
    onSubmit() {
      this.$refs.form.validate(valid => {
        if (!valid) return;
        addRecord({ ...this.form });
        this.$message.success('登记成功');
        this.resetForm();
        this.getDataList();
      });
    },
    resetForm() {
      this.form = { name: '', num: 1, type: '', unit: '', remark: '' };
    },
    getDataList() {
      let list = getRecordList();
      this.totalPage = list.length;
      const start = (this.pageIndex-1)*this.pageSize;
      this.dataList = list.slice(start, start+this.pageSize);
    },
    currentChangeHandle(val) {
      this.pageIndex = val;
      this.getDataList();
    },
    sizeChangeHandle(val) {
      this.pageSize = val;
      this.getDataList();
    },
    goBack() {
      this.$router.push('/kucun');
    }
  }
}
</script>
